layui.use(['table', 'admin', 'ax', 'func', 'laydate'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;
    var laydate = layui.laydate;

    //日期范围
    laydate.render({
        elem: '#searchTime'
        , type: 'date'
        , range: '~'
        , format: 'yyyy-MM-dd'
    });


    /**
     * 浏览记录表管理
     */
    var Browse = {
        tableId: "browseTable"
    };

    /**
     * 初始化表格的列
     */
    Browse.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', align: 'center', hide: true, title: 'ID'},
            {
                field: 'type', align: 'center', sort: true, title: '类型', templet: function (d) {
                    var flag_box = "";
                    if (d.type == 0) {
                        flag_box = "<span class='layui-btn layui-btn-xs layui-btn-normal' >浏览职位</span>";
                    } else if (d.type == 1) {
                        flag_box = "<span class=\"layui-btn layui-btn-xs layui-btn-disabled \">浏览简历</span>";
                    }
                    return flag_box;
                }
            },
            {field: 'memberName', align: 'center', sort: true, title: '用户姓名'},
            {field: 'memberPhone', align: 'center', sort: true, title: '手机号码'},
            {field: 'postName', align: 'center', sort: true, title: '职位名称'},
            {field: 'companyName', align: 'center', sort: true, title: '公司名称'},
            {field: 'memberName', align: 'center', sort: true, title: '简历姓名'},
            {field: 'createTime', align: 'center', sort: true, title: '浏览时间'},
            // {align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };

    /**
     * 点击查询按钮
     */
    Browse.search = function () {
        var queryData = {};
        queryData['keyword'] = $("#condition").val();
        queryData['searchTime'] = $("#searchTime").val();
        table.reload(Browse.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 弹出添加对话框
     */
    Browse.openAddDlg = function () {
        func.open({
            title: '添加浏览记录表',
            content: Feng.ctxPath + '/browse/add',
            tableId: Browse.tableId
        });
    };

    /**
     * 点击编辑
     *
     * @param data 点击按钮时候的行数据
     */
    Browse.openEditDlg = function (data) {
        func.open({
            title: '修改浏览记录表',
            content: Feng.ctxPath + '/browse/edit?id=' + data.id,
            tableId: Browse.tableId
        });
    };

    /**
     * 导出excel按钮
     */
    Browse.exportExcel = function () {
        var checkRows = table.checkStatus(Browse.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    Browse.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/browse/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(Browse.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + Browse.tableId,
        url: Feng.ctxPath + '/browse/list',
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        cols: Browse.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        Browse.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        Browse.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        Browse.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + Browse.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            Browse.openEditDlg(data);
        } else if (layEvent === 'delete') {
            Browse.onDeleteItem(data);
        }
    });
});
